/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package radiostationv41;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.JOptionPane;


/**
 *
 * @author Stella
 */
public class ArtistFormFrame extends javax.swing.JFrame {
    
    private ArtistFrame parentFrame;
    private int epilogi;
    private static EntityManager em;
    private EntityManagerFactory emf;
    private Artist artist;
    
  

    
public ArtistFormFrame (ArtistFrame parentFrame,int epilogi, Artist artist){
    initComponents();
    this.parentFrame= parentFrame;
    this.epilogi= epilogi;
    this.parentFrame.setEnabled(false);
    this.artist= artist;
    
            if (epilogi == 2 || epilogi == 3 ){
                    
               this.lastNameField1.setText(artist.getArtistLastName()); 
               this.firstNameField2.setText(artist.getArtistFirstName());
               this.artisticNameField3.setText(artist.getArtistArtisticName());
               this.artisticNameField3.setEditable(false);
                    if(artist.getArtistSex().equals("Άντρας"))
                        this.sexComboBox1.setSelectedIndex(0);
                            else    
                        this.sexComboBox1.setSelectedIndex(1);
                    
                    for(int i=0;i<this.mGenreComboBox2.getItemCount();i++)
                        if(artist.getMusicGenreName().equals(this.mGenreComboBox2.getItemAt(i).toString()))
                            this.mGenreComboBox2.setSelectedIndex(i);
                    
                    SimpleDateFormat sdf= new SimpleDateFormat("d/MM/YYYY");
                    this.bDateField4.setText(sdf.format(artist.getArtistBirthDay()));
                    this.bDateField4.setText(artist.getArtistBirthPlace());
                    
            }
                    
            if(epilogi ==3){
                
                this.lastNameField1.setEditable(false);
                this.firstNameField2.setEditable(false);
                this.artisticNameField3.setEditable(false);
                this.sexComboBox1.setEnabled(false);
                this.bDateField4.setEditable(false);
                this.bPlaceField5.setEditable(false);
                this.saveButton1.setText("Delete");
                
            }   
                       
            emf = Persistence.createEntityManagerFactory("RadioStationV41PU");
            em = emf.createEntityManager();
            em.getTransaction().begin();
            
            
}
    private void cancelButton2ActionPerformed(java.awt.event.ActionEvent evt){
      parentFrame.setEnabled(true);
      dispose();
    }
    
    private void saveButton1ActionPerformed(java.awt.event.ActionEvent evt){
        String lname = lastNameField1.getText();
        String fname = firstNameField2.getText();
        String artName = artisticNameField3.getText();
        String sex = sexComboBox1.getSelectedItem().toString();
        String bdate = bDateField4.getText();
        String bplace = bPlaceField5.getText();
        String musicGenre = mGenreComboBox2.getSelectedItem().toString();
        Artist artist = null;
        
        
        try {
            
            
            DateFormat format = new SimpleDateFormat("d/MM/YYYY");
            Date date = format.parse(bdate);
            
            //Δημιουργία Νέου Καλλιτέχνη
            
               
            if (epilogi ==1){
                System.out.println(epilogi);
                if(this.artisticNameField3.getText().equals("") || this.firstNameField2.getText().equals("") || this.firstNameField2.getText().equals(""))
                   JOptionPane.showMessageDialog(this,"Παρακαλώ συμπληρώστε τα πεδία");
                else {
                MusicGenre mg=em.find(MusicGenre.class, musicGenre);
                artist= new Artist (fname, lname, artName, sex, format.parse(bdate), bplace,mg);
                
                try{
                   
                    
                    em.persist(artist);
                    em.getTransaction().commit();
                }
                
                catch(Exception e){
                    System.out.println("Η εγγραφή δεν πραγματοποιήθηκε");
                    em.getTransaction().rollback();
                } 
                parentFrame.setEnabled(true);
                //parentFrame.dispose();
                dispose();
                }

            }else if (epilogi == 2){
                        artist= em.find(Artist.class,artName);
                        artist.setArtistLastName(lname);
                        artist.setArtistFirstName(fname);
                        artist.setArtistArtisticName(artName);
                        artist.setArtistSex(sex);
                        artist.setArtistBirthPlace(bplace);
                        artist.setArtistBirthDay(format.parse(bdate));
                        artist.setMusicGenreName(new MusicGenre(musicGenre));   
                        
                        
                        try {
                        
                            em.getTransaction().commit();
                        
                        } catch(Exception e){
                        System.out.println("Προσπαθήστε ξανά");
                        }
                        
                        
             parentFrame.setEnabled(true);
                //parentFrame.dispose();
                dispose();           
            }  else {
                        
                        //Διαγραφή Καλλιτέχνη
                        
                        artist = em. find(Artist.class,artName);
                        em.remove (artist);
                        em.getTransaction().commit();
                            parentFrame.setEnabled(true);
                //parentFrame.dispose();
                dispose();
            }
            
                
                
        }    
            catch(ParseException parseEx){
                    JOptionPane.showMessageDialog(this, "Λάθος μορφή ημερομηνίας");
                    }    

        
    }        
                
            
         
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        RadioStationV41PUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("RadioStationV41PU").createEntityManager();
        musicGenreQuery1 = java.beans.Beans.isDesignTime() ? null : RadioStationV41PUEntityManager.createQuery("SELECT m.musicGenreName FROM MusicGenre m");
        musicGenreList1 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : musicGenreQuery1.getResultList();
        jPanel1 = new javax.swing.JPanel();
        LnameLabel1 = new javax.swing.JLabel();
        lastNameField1 = new javax.swing.JTextField();
        fNameLabel2 = new javax.swing.JLabel();
        firstNameField2 = new javax.swing.JTextField();
        aNameLabel3 = new javax.swing.JLabel();
        artisticNameField3 = new javax.swing.JTextField();
        sexLabel4 = new javax.swing.JLabel();
        sexComboBox1 = new javax.swing.JComboBox();
        bDateLabel5 = new javax.swing.JLabel();
        bDateField4 = new javax.swing.JTextField();
        bPlaceLabel6 = new javax.swing.JLabel();
        bPlaceField5 = new javax.swing.JTextField();
        mGenreLabel7 = new javax.swing.JLabel();
        mGenreComboBox2 = new javax.swing.JComboBox();
        saveButton1 = new javax.swing.JButton();
        cancelButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Καλλιτέχνες", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Calibri", 1, 18))); // NOI18N

        LnameLabel1.setText("Επώνυμο");

        fNameLabel2.setText("Όνομα");

        aNameLabel3.setText("Καλλιτεχνικό Όνομα");

        sexLabel4.setText("Φύλλο");

        sexComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Άνδρας", "Γυναίκα" }));

        bDateLabel5.setText("Ημερομηνία Γέννησης");

        bPlaceLabel6.setText("Τόπος Γέννησης");

        mGenreLabel7.setText("Είδος Μουσικής");

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, musicGenreList1, mGenreComboBox2);
        bindingGroup.addBinding(jComboBoxBinding);

        saveButton1.setText("Αποθήκευση");
        saveButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButton1ActionPerformed(evt);
            }
        });

        cancelButton2.setText("Ακύρωση");
        cancelButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(LnameLabel1)
                    .addComponent(fNameLabel2)
                    .addComponent(aNameLabel3)
                    .addComponent(sexLabel4)
                    .addComponent(bDateLabel5)
                    .addComponent(bPlaceLabel6)
                    .addComponent(mGenreLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lastNameField1)
                    .addComponent(firstNameField2)
                    .addComponent(artisticNameField3)
                    .addComponent(sexComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bDateField4)
                    .addComponent(bPlaceField5)
                    .addComponent(mGenreComboBox2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(saveButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                        .addComponent(cancelButton2)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LnameLabel1)
                    .addComponent(lastNameField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fNameLabel2)
                    .addComponent(firstNameField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(aNameLabel3)
                    .addComponent(artisticNameField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sexLabel4)
                    .addComponent(sexComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bDateLabel5)
                    .addComponent(bDateField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bPlaceLabel6)
                    .addComponent(bPlaceField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(mGenreLabel7)
                    .addComponent(mGenreComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton2)
                    .addComponent(saveButton1)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel LnameLabel1;
    private javax.persistence.EntityManager RadioStationV41PUEntityManager;
    private javax.swing.JLabel aNameLabel3;
    private javax.swing.JTextField artisticNameField3;
    private javax.swing.JTextField bDateField4;
    private javax.swing.JLabel bDateLabel5;
    private javax.swing.JTextField bPlaceField5;
    private javax.swing.JLabel bPlaceLabel6;
    private javax.swing.JButton cancelButton2;
    private javax.swing.JLabel fNameLabel2;
    private javax.swing.JTextField firstNameField2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField lastNameField1;
    private javax.swing.JComboBox mGenreComboBox2;
    private javax.swing.JLabel mGenreLabel7;
    private java.util.List<radiostationv41.MusicGenre> musicGenreList1;
    private javax.persistence.Query musicGenreQuery1;
    private javax.swing.JButton saveButton1;
    private javax.swing.JComboBox sexComboBox1;
    private javax.swing.JLabel sexLabel4;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
